package com.th.supcom.pds.config.client.widget;

import java.util.List;

import net.carefx.framework.container.client.context.AppContext;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.config.client.common.PdsConfigConstant;
import com.th.supcom.pds.config.client.common.PhamConfigServiceFactory;
import com.th.supcom.pds.config.client.common.PhamDispensaryConfigModuleEvents;
import com.th.supcom.pds.config.client.model.PhamDispensaryConfigModel;
import com.th.supcom.pds.config.client.service.PhamDispensaryConfigRpcAsync;

public class PhamDispensaryConfigAction
{

    private static final PhamDispensaryConfigRpcAsync s_PHAM_DISPENSARY_CONFIG_ASYNC = PhamConfigServiceFactory.getPhamDispensaryConfigService ();

    /**
     * 删除记录
     * 
     * @param configModel
     */
    private void doRemovePhamDispensaryConfig (final PhamDispensaryConfigModel configModel,
                                               final PhamDispensaryConfigMainPanel panel)
    {
        MessageBox.confirm ("", "确定删除【" + configModel.getPhamDispensaryName () + "】属性配置吗？",
                            new Listener <MessageBoxEvent> ()
                            {
                                @Override
                                public void handleEvent (final MessageBoxEvent be)
                                {
                                    verifyDelete (configModel, be);
                                }

                                private void verifyDelete (final PhamDispensaryConfigModel configModel,
                                                           final MessageBoxEvent be)
                                {
                                    if (Dialog.YES.equals (be.getButtonClicked ().getItemId ()))
                                    {
                                        PhamDispensaryConfigAction.s_PHAM_DISPENSARY_CONFIG_ASYNC.removePhamDispensaryConfig (configModel.getPhamDispensaryConfigId (),
                                                                                                                              new AsyncCallback <Void> ()
                                                                                                                              {

                                                                                                                                  @Override
                                                                                                                                  public void onFailure (final Throwable caught)
                                                                                                                                  {
                                                                                                                                      MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                                                        caught.getMessage (),
                                                                                                                                                        null);
                                                                                                                                  }

                                                                                                                                  @Override
                                                                                                                                  public void onSuccess (final Void result)
                                                                                                                                  {
                                                                                                                                      Info.display (PdsConfigConstant.INFO,
                                                                                                                                                    "删除药房属性成功");
                                                                                                                                      fireSearchEvent (panel);
                                                                                                                                  }
                                                                                                                              });
                                    }
                                }
                            });
    }

    /**
     * 编辑药房属性
     * 
     * @param configModel
     */
    private void editPhamDispensaryConfig (final EditPhamDispensaryConfigWindow editWindow,
                                           final PhamDispensaryConfigMainPanel panel)
    {
        editWindow.addListener (PhamDispensaryConfigModuleEvents.UI_REFRESH, new Listener <BaseEvent> ()
        {

            @Override
            public void handleEvent (final BaseEvent be)
            {
                refreshData (getSearchCondition (), panel);
            }
        });
    }

    public void enableBtnListener (final PhamDispensaryConfigModel updateModel,
                                   final PhamDispensaryConfigMainPanel panel)
    {
        PhamDispensaryConfigAction.s_PHAM_DISPENSARY_CONFIG_ASYNC.modifyPhamDispensaryConfigByExample (updateModel,
                                                                                                       new AsyncCallback <Void> ()
                                                                                                       {

                                                                                                           @Override
                                                                                                           public void onFailure (final Throwable caught)
                                                                                                           {
                                                                                                               MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                                 caught.getMessage (),
                                                                                                                                 null);
                                                                                                           }

                                                                                                           @Override
                                                                                                           public void onSuccess (final Void result)
                                                                                                           {
                                                                                                               Info.display (PdsConfigConstant.INFO,
                                                                                                                             "更新药房属性成功");
                                                                                                               fireSearchEvent (panel);
                                                                                                           }
                                                                                                       });
    }

    public final void fireSearchEvent (final PhamDispensaryConfigMainPanel panel)
    {
        final PhamDispensaryConfigModel model = getSearchCondition ();
        final AppEvent appEvent = new AppEvent (PhamDispensaryConfigModuleEvents.UI_REFRESH, model);
        panel.fireEvent (PhamDispensaryConfigModuleEvents.UI_REFRESH, appEvent);
    }

    private PhamDispensaryConfigModel getSearchCondition ()
    {
        final PhamDispensaryConfigModel model = new PhamDispensaryConfigModel ();
        model.setPhamDispensaryCode (ClientUtils.getOrgModel().getOrgCode());

        // 根据控制面板获取查询条件
        return model;
    }

    /**
     * 获取选中行
     * 
     * @return
     */
    private PhamDispensaryConfigModel getSelectedModel (final PhamDispensaryConfigMainPanel panel)
    {
        PhamDispensaryConfigModel configModel = panel.getDispensaryGrid ().getSelectionModel ()
                                                           .getSelectedItem ();
        return configModel;
    }

    /**
     * 刷新数据
     * 
     * @param configModel
     */
    private void refreshData (final PhamDispensaryConfigModel configModel, final PhamDispensaryConfigMainPanel panel)
    {
        panel.getDispensaryGrid ().mask (PdsConfigConstant.GRID_DEFAULT_LOAD_MESSAGE);
       
        PhamDispensaryConfigAction.s_PHAM_DISPENSARY_CONFIG_ASYNC.findPhamDispensaryConfigByExample (configModel,
                                                                                                     new AsyncCallback <List <PhamDispensaryConfigModel>> ()
                                                                                                     {
                                                                                                         @Override
                                                                                                         public void onFailure (final Throwable caught)
                                                                                                         {
                                                                                                             panel.getDispensaryGrid ()
                                                                                                                  .unmask ();
                                                                                                             MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                               caught.getMessage (),
                                                                                                                               null);
                                                                                                         }

                                                                                                         @Override
                                                                                                         public void onSuccess (final List <PhamDispensaryConfigModel> result)
                                                                                                         {
                                                                                                             panel.getListStore ()
                                                                                                                  .removeAll ();
                                                                                                             panel.getListStore ()
                                                                                                                  .add (result);
                                                                                                             panel.getDispensaryGrid ()
                                                                                                                  .unmask ();
                                                                                                             
                                                                                                         }
                                                                                                     });
    }

    /**
     * 注册监听事件.
     * 
     */
    public void registerListeners (final PhamDispensaryConfigMainPanel panel)
    {
        panel.addListener (PhamDispensaryConfigModuleEvents.UI_REFRESH, new Listener <AppEvent> ()
        {

            @Override
            public void handleEvent (final AppEvent be)
            {
                final PhamDispensaryConfigModel model = be.getData ();
                refreshData (model, panel);
            }
        });
        panel.getRefreshButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                fireSearchEvent (panel);
            }
        });
        panel.getEditButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                PhamDispensaryConfigModel configModel=getSelectedModel (panel);
                
                final EditPhamDispensaryConfigWindow editWindow = new EditPhamDispensaryConfigWindow (configModel);
                editWindow.setHeading ("编辑药房属性");
                editWindow.show ();
                editPhamDispensaryConfig (editWindow, panel);
            }

        });
        panel.getAddButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
                                                    {
                                                        @Override
                                                        public void componentSelected (final ButtonEvent ce)
                                                        {
                                                            final PhamDispensaryConfigModel configModel = getLocalModel ();
                                                            final EditPhamDispensaryConfigWindow editWindow = new EditPhamDispensaryConfigWindow (configModel);
                                                            editWindow.setHeading ("新增药房属性");
                                                            editWindow.show ();
                                                            editPhamDispensaryConfig (editWindow, panel);
                                                        }

                                                    });
       
        panel.getRemoveButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final PhamDispensaryConfigModel configModel = getSelectedModel (panel);
                doRemovePhamDispensaryConfig (configModel, panel);
            }
        });
        panel.getDispensaryGrid ().addListener (Events.CellDoubleClick,
                                                new Listener <GridEvent <PhamDispensaryConfigModel>> ()
                                                {

                                                    @Override
                                                    public void handleEvent (final GridEvent <PhamDispensaryConfigModel> be)
                                                    {
                                                        final EditPhamDispensaryConfigWindow editWindow = new EditPhamDispensaryConfigWindow (
                                                                                                                                              be.getModel ());
                                                        editWindow.setHeading ("编辑药房属性");
                                                        editWindow.show ();
                                                        editPhamDispensaryConfig (editWindow, panel);
                                                    }
                                                });
        new KeyNav <ComponentEvent> (panel)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                if (ce.isAltKey ())
                {
                    shortCutKey (ce.getKeyCode (), panel);
                }
            }
        };
        refreshData (getSearchCondition (), panel);
    }

    /**
     * 快捷键
     * 
     * @param keyCode
     */
    private void shortCutKey (final int keyCode, final PhamDispensaryConfigMainPanel panel)
    {
        if (keyCode == KeysConstants.VK_D)
        {
            final PhamDispensaryConfigModel configModel = getSelectedModel (panel);
            doRemovePhamDispensaryConfig (configModel, panel);
        }
        if (keyCode == KeysConstants.VK_F)
        {
            fireSearchEvent (panel);
        }
        if (keyCode == KeysConstants.VK_A)
        {
            final PhamDispensaryConfigModel configModel=getLocalModel ();
            final EditPhamDispensaryConfigWindow editWindow = new EditPhamDispensaryConfigWindow (configModel);
            editWindow.setHeading ("新增药房属性");
            editWindow.show ();
            editPhamDispensaryConfig (editWindow, panel);
        }
        if (keyCode == KeysConstants.VK_X)
        {
            final PhamDispensaryConfigModel configModel = getSelectedModel (panel);
            final EditPhamDispensaryConfigWindow editWindow = new EditPhamDispensaryConfigWindow (configModel);
            editWindow.setHeading ("编辑药房属性");
            editWindow.show ();
            editPhamDispensaryConfig (editWindow, panel);
        }
    }
    /**
     * 得到本科室编码和名称
     * @return
     * add by Zli
     */
    private PhamDispensaryConfigModel getLocalModel ()
    {
        final PhamDispensaryConfigModel configModel=new PhamDispensaryConfigModel();
        configModel.setPhamDispensaryCode (ClientUtils.getOrgModel().getOrgCode());
        configModel.setPhamDispensaryName (ClientUtils.getOrgModel().getOrgName());
        return configModel;
    }
}
